import time

import requests
from urllib3.exceptions import NewConnectionError
from PySide6.QtCore import QThread, Signal
from settings import HOST


class WechatUserInfoThread(QThread):
    error_signal = Signal(str)
    success_signal = Signal(dict)

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.try_seconds = 0

    def run(self):
        while True:
            time.sleep(1)
            try:
                r = requests.post(HOST, json={'type': 3})
                data = r.json()
                if data['errno'] == 0:
                    self.success_signal.emit(data['data'])
                    break
            except NewConnectionError as e:
                self.try_seconds += 1
                if self.try_seconds < 60:
                    continue
                else:
                    self.error_signal.emit(str(e))
                    break
            except Exception as e:
                self.error_signal.emit(str(e))
